import java.util.ArrayDeque;
import java.util.Queue;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28318
 * Date: 2025-11-13
 * Time: 18:36
 */
class MyStack {

    int useSize;
    Queue<Integer> q1;
    Queue<Integer> q2;

//    构造方法
    public MyStack() {
        q1 = new ArrayDeque<>();
        q2 = new ArrayDeque<>();
    }

//    入栈/压栈
    public void push(int x) {
        if (q2.isEmpty()){
            q1.offer(x);
        } else if (q1.isEmpty()) {
            q2.offer(x);
        }
        this.useSize++;
    }

//    出栈
    public int pop() {
        if (this.empty()){
            return -1;
        }
        int ret = 0;
        if (q2.isEmpty()){
            for (int i = 0; i < this.useSize - 1; i++) {
                q2.offer(q1.poll());
            }
            this.useSize--;
            ret = q1.poll();
        } else if (q1.isEmpty()) {
            for (int i = 0; i < this.useSize - 1; i++) {
                q1.offer(q2.poll());
            }
            this.useSize--;
            ret = q2.poll();
        }
        return ret;
    }

//    获取栈顶元素
    public int top() {
        if (this.empty()){
            return -1;
        }
        int ret = 0;
        if (q2.isEmpty()){
            for (int i = 0; i < this.useSize - 1; i++) {
                q2.offer(q1.poll());
            }
            ret = q1.peek();
            q2.offer(q1.poll());
        } else if (q1.isEmpty()) {
            for (int i = 0; i < this.useSize - 1; i++) {
                q1.offer(q2.poll());
            }
            ret = q2.peek();
            q1.offer(q2.poll());
        }
        return ret;
    }

//    判断栈是否为空
    public boolean empty() {
        return this.useSize == 0;
    }
}
